#include "mem.h"

typedef struct
{
	int id;
	char buff[MEMBLOCKS][BUFF_SIZE];
}MEM_CONTEXT;

MEM_CONTEXT* g_mem = 0;

VIDEO_ERROR mem_init()
{
	if (g_mem)
	{
		return VIDEO_ERROR_01;
	}
	g_mem = (MEM_CONTEXT*)MALLOC(sizeof(MEM_CONTEXT));
	if (g_mem == 0)
	{
		return VIDEO_ERROR_02;
	}
	assert(sizeof(g_mem->buff[0]) == BUFF_SIZE);
	memset(g_mem, 0, sizeof(MEM_CONTEXT));
	return VIDEO_ERROR_OK;
}

VIDEO_ERROR mem_free()
{
	if (g_mem)
	{
		FREE(g_mem);
		g_mem = 0;
	}
	return VIDEO_ERROR_OK;
}

void* mem_get_buff()
{
	void* ret = &g_mem->buff[g_mem->id];
	g_mem->id++;
	g_mem->id %= MEMBLOCKS;
	return ret;
}
